package org.col.dspace.tagger.model;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;

import org.col.dspace.tagger.OnlineKeyphraseExtractor;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;

public abstract class AbstractTagGenerator implements TagGenerator{

	protected Bitstream bstream;
	public AbstractTagGenerator(Bitstream bstream){
		this.bstream = bstream;
		
	}
	//step 1
	protected InputStream processBitStream() throws IOException, SQLException, AuthorizeException{
		return bstream.retrieve();
	}
	

	//step 2
	abstract protected String retrieveText(InputStream istream) throws IOException;
	
	//step 3
	protected String processTextStream(String text){
		return text;
	}
	
	//step 4
	protected int extractTags(String text, ArrayList<String> tags){
		OnlineKeyphraseExtractor ke = new OnlineKeyphraseExtractor();
		ke.setDefaultOptions();
		try {
			ke.loadModel();
			ke.extractKeyPhrases(text,tags);
		} catch (Exception e) {
			e.printStackTrace();
			return STATUS_TAG_GEN_FAILED;
		}
			
		return STATUS_TAG_GEN_SUCCESS;
	}
	
	@Override
	public int generateTags(ArrayList<String> tags) throws IOException,
			SQLException, AuthorizeException {
		
		
		String retrievedText = retrieveText(processBitStream());
	
		String processedText =processTextStream(retrievedText);

		int status= extractTags(processedText,tags);

		return status;
		
		

		
	}
	

}
